package com.common.widget.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.product.android.business.ApplicationVariable;
import com.starappallcommon.R;

/**
 * 发起群聊/添加朋友下拉弹出框 <br>
 * Created 2014-5-15 下午1:56:43
 * 
 * @version
 * @author linsj
 * 
 * @see
 */
public class CommonPopup extends PopupWindow {

    /** 动画开始位置比率 */
    private final float ANIMATE_FROM_ALPHA = 0.1f;

    /** 动画结束位置比率 */
    private final float ANIMATE_TO_ALPHA = 1.0f;

    /** 动画展示时间，毫秒 */
    private final int ANIMATE_DURATION = 100;

    /** 界面根元素 */
    protected View root;

    /** 布局装载对象 */
    protected final LayoutInflater inflater;

    /** 上下文 */
    protected final Context mContext;

    /** 下拉框的父view */
    protected final View anchor;

    /** 弹出框 */
    protected final PopupWindow window;

    /** 窗口管理类 */
    protected final WindowManager windowManager;

    /** 用户操作适配器 */
    private BaseAdapter mAdapter;

    /** 用户列表 */
    private ListView lvUserList;

    /** 回调接口 */
    private IOnOper mOper;
    /** 监听回调 */
    private CommonPopupListener mCommonPopupListener;

    /**
     * 聊天方式操作回调 <br>
     * Created 2014-5-15 下午2:46:30
     * 
     * @version SelectUserAdapter
     * @author linsj
     * 
     * @see
     */
    public interface IOnOper {
        /**
         * 用户的操作接口 <br>
         * Created 2014-5-15 下午2:46:45
         * 
         * @param iAction
         *            操作类型
         * @author : linsj
         */
        void onAction(int iAction);
    }

    /**
     * 构造方法 Creates a new instance of SelectUserPopup. <br>
     * Created 2014-5-15 下午2:05:01
     * 
     * @param mAnchor
     *            父VIEW
     * @param adapter
     *            适配器
     * @param oper
     *            用户操作回调
     */
    public CommonPopup(View mAnchor, BaseAdapter adapter, IOnOper oper) {
        super(mAnchor);
        mOper = oper;
        anchor = mAnchor;

        window = new PopupWindow(ApplicationVariable.INSTANCE.applicationContext);

        window.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int i = event.getAction();
                if (i == MotionEvent.ACTION_OUTSIDE) {
                    window.setFocusable(false);
                    window.dismiss();
                    return true;
                }
                if (i == MotionEvent.ACTION_DOWN) {
                    dismissThis();
                }
                return false;
            }
        });

        // 得到一个windowManager对象，用来得到窗口的一些属性
        windowManager = (WindowManager) anchor.getContext()
                .getSystemService(Context.WINDOW_SERVICE);

        mContext = mAnchor.getContext();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        initView();

        mAdapter = adapter;
        lvUserList.setAdapter(mAdapter);
    }

    /**
     * 抽象初始化设置布局，继承该类需设置布局文件
     * 
     * <br>
     * Created 2014-5-15 下午1:58:43
     * 
     * @author : linsj
     */
    protected void initView() {
        root = (ViewGroup) inflater.inflate(R.layout.common_list_popup, null);
        root.setFocusableInTouchMode(true);
        root.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_MENU) && (window.isShowing())) {
                    window.setFocusable(false);
                    dismissThis();
                    window.dismiss();
                    return true;
                }
                return false;
            }
        });
        lvUserList = (ListView) root.findViewById(R.id.lv_select_popup);
        setContentView(root);

        lvUserList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                window.setFocusable(false);
                dismissThis();
                window.dismiss();
                mOper.onAction(position);
            }
        });
    }

    /** 是否弹出下拉框 */
    private boolean isDisplay = true;

    /**
     * 弹出窗体
     * 
     * <br>
     * Created 2014-5-15 下午2:06:07
     * 
     * @param xoffset
     *            显示位置横坐标偏移量修正,
     * @param yoffset
     *            纵坐标修正
     * @author : linsj
     */
    public void show(int xoffset, int yoffset) {
        // 预处理，设置window
        preShow();
        if (!isDisplay) {
            return;
        }
        int[] location = new int[2];
        // 得到anchor的位置
        anchor.getLocationOnScreen(location);

        // root.setLayoutParams(new LayoutParams(300,
        // LayoutParams.WRAP_CONTENT));

        // 在指定位置弹出弹窗
        window.showAsDropDown(anchor, 0 + xoffset, 0 + yoffset);

        AlphaAnimation aa = new AlphaAnimation(ANIMATE_FROM_ALPHA, ANIMATE_TO_ALPHA);
        aa.setDuration(ANIMATE_DURATION);
        root.startAnimation(aa);
    }

    /**
     * 预处理窗口
     * 
     * <br>
     * Created 2014-5-15 下午2:06:41
     * 
     * @author : linsj
     */
    protected void preShow() {
        // 背景是唯一能确定popupwindow宽高的元素，这里使用root的背景，但是需要给popupwindow设置一个空的BitmapDrawable
        window.setBackgroundDrawable(new BitmapDrawable());
        int width = anchor.getMeasuredWidth();
        // 设置宽度
        window.setWidth(width);
        window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setTouchable(true);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        // 指定布局
        window.setContentView(root);
    }

    /**
     * 关闭弹窗方法 <br>
     * Created 2014-9-22 下午5:33:46
     * 
     * @author gpy
     */
    private void dismissThis() {
        if (mCommonPopupListener != null) {
            mCommonPopupListener.onDismiss();
        }
       

    }

    /**
     * 设置监听 <br>
     * Created 2014-9-22 下午5:31:34
     * 
     * @param commonPopupListener
     *            监听处理
     * @author gpy
     */
    public void setCommonPopupListener(CommonPopupListener commonPopupListener) {
        mCommonPopupListener = commonPopupListener;
    }

    /**
     * 弹窗监听 <br>
     * Created 2014-9-22 下午5:28:09
     * 
     * @version CommonPopup
     * @author gpy
     * 
     * @see
     * 
     *      Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
     * 
     */
    public interface CommonPopupListener {
        /**
         * 弹窗消失时监听 <br>
         * Created 2014-9-22 下午5:28:59
         * 
         * @author gpy
         */
        void onDismiss();
    }

}
